package cn.ulc.socket.weapp.service.impl;

import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.util.CalendarUtil;
import cn.ulc.socket.weapp.common.BaseService;
import cn.ulc.socket.weapp.model.dto.FavoriteGoodsListDTO;
import cn.ulc.socket.weapp.model.dto.UserIndexDTO;
import cn.ulc.socket.weapp.model.dto.WxUserDTO;
import cn.ulc.socket.weapp.model.param.UserSaveParam;
import cn.ulc.socket.weapp.service.UserServ;
import cn.ulc.store.dao.order.OrderDao;
import cn.ulc.store.dao.sign.SignDao;
import cn.ulc.store.dao.sign.SignRecordDao;
import cn.ulc.store.dao.staff.StaffDao;
import cn.ulc.store.dao.user.MemberDao;
import cn.ulc.store.dao.user.UserCouponDao;
import cn.ulc.store.dao.user.UserFavoriteGoodsDao;
import cn.ulc.store.dao.user.WeixinUserDao;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.sign.Sign;
import cn.ulc.store.entity.sign.SignRecord;
import cn.ulc.store.entity.staff.Staff;
import cn.ulc.store.entity.user.Member;
import cn.ulc.store.entity.user.UserCoupon;
import cn.ulc.store.entity.user.WeixinUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class UserServImpl extends BaseService implements UserServ {
    SignDao signDao;
    UserCouponDao userCouponDao;
    OrderDao orderDao;
    SignRecordDao recordDao;
    MemberDao memberDao;
    WeixinUserDao userDao;
    StaffDao staffDao;
    UserFavoriteGoodsDao favoriteGoodsDao;
    @Override
    public UserIndexDTO loadIndex() {
        UserIndexDTO dto = new UserIndexDTO();
        WeixinUser loginUser = getCurrentUser();
        Company company = getCurrentCompany();
        Sign sign = signDao.findByUserId(loginUser.getId());
        String date = CalendarUtil.dateToString(new Date(),CalendarUtil.SDF_DEFAULT);
        SignRecord signRecord = recordDao.findByDate(loginUser.getId(),date);
        List<UserCoupon> couponList = userCouponDao.findListByUserId(loginUser.getId(),PubDataConstant.COUPON_STATUS_USING,false);
        List<Map<String,Object>> countList = orderDao.findCountByStatus(loginUser.getId());
        Member member = memberDao.findByWxUserId(loginUser.getId());
        Staff staff = staffDao.findRiderByWxUserId(company.getId(),loginUser.getId(),PubDataConstant.TYPE_ID_RIDER);
        if(staff!=null){
            dto.setIsRider(true);
        }else{
            dto.setIsRider(false);
        }
        dto.setMember(member);
        dto.setSign(sign);
        dto.setSignRecord(signRecord);
        dto.setCouponList(couponList);
        dto.setCountList(countList);
        return dto;
    }

    @Override
    public WxUserDTO update(UserSaveParam param) {
        WxUserDTO dto = new WxUserDTO();
        WeixinUser loginUser = getCurrentUser();
        loginUser.setRealName(param.getRealName());
        loginUser.setBirthDay(param.getBirthDay());
        userDao.save(loginUser);
        dto.setUser(loginUser);
        Member member = memberDao.findByWxUserId(loginUser.getId(),AvailableStatus.AVAILABLE);
        if(member!=null){
            member.setBirthday(loginUser.getBirthDay());
            member.setName(loginUser.getRealName());
            memberDao.save(member);
            dto.setMember(member);
        }
        return dto;
    }

    @Override
    public FavoriteGoodsListDTO findFavoriteGoodsList() {
        WeixinUser loginUser = getCurrentUser();

        return null;
    }
}
